package com.yj.oa.mapper;

import com.yj.contract.dto.ContractChangeStatusDto;
import com.yj.oa.dto.oa.MainIDDto;
import com.yj.oa.dto.oa.UpdateStatusDto;
import com.yj.oa.entity.system.OAContract;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/4
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface OAContractMapper {


    @Insert("INSERT INTO contract " +
            "(main_id, username, user_id, businessline, contract_name, contract_status, contract_code, " +
            "contract_amount, contract_sum, transport_type, caigou, gongying, zhongduan, subtime ) " +
            "VALUES(#{item.main_id}, #{item.username}, #{item.user_id}, #{item.businessline}, #{item.contract_name}, " +
            "#{item.contract_status}, #{item.contract_code}, #{item.contract_amount}, #{item.contract_sum}, " +
            "#{item.transport_type}, #{item.caigou}, #{item.gongying}, #{item.zhongduan}, #{item.subtime})")
    void addContract(@Param("item") OAContract item);

    @Select("SELECT COUNT(*) FROM contract WHERE main_id = #{main_id}")
    Integer getContractCountByMainID(@Param("main_id") String main_id);

    @Select("SELECT * FROM contract WHERE main_id = #{main_id} LIMIT 1")
    OAContract getContractByMainID(@Param("main_id") String main_id);

    @Select("<script>" +
            "SELECT * FROM contract WHERE main_id IN " +
            "<foreach item='item' collection='list' open='(' separator=', ' close=')'>" +
            "#{item.main_id}" +
            "</foreach>" +
            "</script>")
    List<OAContract> getByTransferList(@Param("list") List<MainIDDto> list);

    @Select("<script>" +
            "SELECT COUNT(*) FROM contract " +
            "WHERE user_id IN " +
            "<foreach item = 'item' collection = 'list' open = '(' separator = ', ' close = ') '>" +
            "#{item}" +
            "</foreach>" +
            "AND contract_status = 2 AND system_contract_status = 2" +
            "</script>")
    int countForDeliveringContract(@Param("list") List<String> list);

    @Select("<script>" +
            "SELECT COUNT(*) FROM contract " +
            "WHERE user_id IN " +
            "<foreach item = 'item' collection = 'list' open = '(' separator = ', ' close = ') '>" +
            "#{item}" +
            "</foreach>" +
            "AND contract_status = 2 AND system_contract_status = 3 " +
            "</script>")
    int countForFinishedContract(@Param("list") List<String> list);

    @Update("UPDATE contract SET username = #{item.username}, user_id = #{item.user_id}, businessline = #{item.businessline}, contract_name = #{item.contract_name}, " +
            "contract_status = #{item.contract_status}, contract_code = #{item.contract_code}, contract_amount = #{item.contract_amount}, " +
            "contract_sum = #{item.contract_sum}, transport_type = #{item.transport_type}, caigou= #{item.caigou}, gongying = #{item.gongying}, " +
            "zhongduan = #{item.zhongduan}, subtime= #{item.subtime} " +
            "WHERE main_id = #{item.main_id}")
    void updateContract(@Param("item") OAContract item);

    @Update("UPDATE contract SET contract_status = #{item.contract_status} " +
            "WHERE main_id = #{item.main_id}")
    void updateStatus(@Param("item") UpdateStatusDto item);

    @Update("UPDATE contract SET system_contract_status = #{item.status} " +
            "WHERE main_id = #{item.main_id}")
    void updateSystemStatus(@Param("item") ContractChangeStatusDto item);

    @Update("UPDATE contract SET username = #{username}, user_id = #{user_id} WHERE main_id = #{main_id}")
    void updateOwnership(@Param("main_id") String main_id,
                         @Param("username") String username,
                         @Param("user_id") String user_id);

}
